package das_java_forum_ide;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.FlowLayout;

import javax.swing.JMenuBar;
import javax.swing.JToolBar;
import javax.swing.JMenu;
import javax.swing.BoxLayout;
import javax.swing.UIManager;

import java.awt.Component;

import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.JEditorPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.KeyStroke;

import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;



public class Main extends JFrame {

	private JPanel contentPane;
	private JSplitPane splitPane;
	private static DefaultTreeModel model;
	private JTree tree;
	public static String WorkingDir;
	public JLabel pathlbl;
	private JTabbedPane tabbedPane;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					
					UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
					
					
					Main frame = new Main();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Main() {
		setTitle("das-java-forum.com -  IDE");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1234, 869);
		
		
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnDatei = new JMenu("Datei");
		menuBar.add(mnDatei);
		
		JMenuItem mntmNeuesProjekt = new JMenuItem("Neues Projekt");
		mntmNeuesProjekt.setIcon(new ImageIcon(Main.class.getResource("/das_java_forum_ide/pic/document-new-4.png")));
		mntmNeuesProjekt.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				
				neuesprojekt f = new neuesprojekt();
				f.path = WorkingDir;
				f.setVisible(true);
						
				
			}
		});
		mntmNeuesProjekt.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
		mnDatei.add(mntmNeuesProjekt);
		
		JMenuItem mntmProjektffnen = new JMenuItem("Projekt \u00D6ffnen");
		mntmProjektffnen.setIcon(new ImageIcon(Main.class.getResource("/das_java_forum_ide/pic/document-open-4.png")));
		mntmProjektffnen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
		mnDatei.add(mntmProjektffnen);
		
		JSeparator separator = new JSeparator();
		mnDatei.add(separator);
		
		JMenuItem mntmSchlieen = new JMenuItem("Schlie\u00DFen");
		mntmSchlieen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
		mnDatei.add(mntmSchlieen);
		
		JMenuItem mntmAlleSchlieen = new JMenuItem("Alle Schlie\u00DFen");
		mntmAlleSchlieen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK | InputEvent.ALT_MASK));
		mnDatei.add(mntmAlleSchlieen);
		
		JSeparator separator_1 = new JSeparator();
		mnDatei.add(separator_1);
		
		JMenuItem mntmSpeichern = new JMenuItem("Speichern");
		mntmSpeichern.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
		mnDatei.add(mntmSpeichern);
		
		JMenuItem mntmSpeichernUnter = new JMenuItem("Speichern Unter");
		mntmSpeichernUnter.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK));
		mnDatei.add(mntmSpeichernUnter);
		
		JSeparator separator_3 = new JSeparator();
		mnDatei.add(separator_3);
		
		JMenuItem mntmNeuesVerzeichniss = new JMenuItem("Neues Verzeichniss");
		mntmNeuesVerzeichniss.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				NeuesVerzeichniss nv = new NeuesVerzeichniss();
				nv.setVisible(true);
				
			}
		});
		mnDatei.add(mntmNeuesVerzeichniss);
		
		JMenu mnProjekt = new JMenu("Projekt");
		menuBar.add(mnProjekt);
		
		JMenuItem mntmNeueKlasse = new JMenuItem("Neues Element Hinzuf\u00FCgen");
		mntmNeueKlasse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				newfile f = new newfile();
				f.setVisible(true);
				
			}
		});
		mnProjekt.add(mntmNeueKlasse);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("JFrame");
		mnProjekt.add(mntmNewMenuItem);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JToolBar toolBar = new JToolBar();
		contentPane.add(toolBar, BorderLayout.NORTH);
		
		
		
		splitPane = new JSplitPane();
		splitPane.setDividerLocation(250);
		contentPane.add(splitPane, BorderLayout.CENTER);
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		splitPane.setRightComponent(tabbedPane);
		
		JScrollPane scrollPane = new JScrollPane();
		tabbedPane.addTab("Wilkommen", null, scrollPane, null);
		
		JEditorPane editorPane = new JEditorPane();
		scrollPane.setViewportView(editorPane);
		
		
		model = new DefaultTreeModel(new DefaultMutableTreeNode("Projekte"));
		
		tree = new JTree(model);
		tree.addTreeSelectionListener(new TreeSelectionListener() {
			public void valueChanged(TreeSelectionEvent arg0) {
				TreePath nodepath = tree.getSelectionPath();
				String path = WorkingDir;
				
				for(int i = 0 ; i < nodepath.getPathCount() -1 ; i++ ){
					
						path = path + "\\" + nodepath.getPathComponent(i + 1);
						
				}
				
				System.out.println(path);
				File f = new File(path);
				if(f.isFile()){
					
					loadFile(f);
					
				}
				
			}
		});
		tree.setBorder(new CompoundBorder());
		splitPane.setLeftComponent(tree);
		
		
		JButton btnNeu = new JButton("Neuer Tab");
		btnNeu.setIcon(new ImageIcon(Main.class.getResource("/das_java_forum_ide/pic/document-new-4.png")));
		btnNeu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				JScrollPane tempPane = new JScrollPane();
				JEditorPane tempeditor = new JEditorPane();
				tempPane.setViewportView(tempeditor);
				tabbedPane.add("Neue Datei",tempPane);
				
			}
		});
		toolBar.add(btnNeu);
		
		JButton btnSchlieen = new JButton("Tab Schlie\u00DFen");
		btnSchlieen.setIcon(new ImageIcon(Main.class.getResource("/das_java_forum_ide/pic/document-close.png")));
		btnSchlieen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				tabbedPane.remove(tabbedPane.getSelectedIndex());
				
			}
		});
		toolBar.add(btnSchlieen);
		
		JButton btnDebugen = new JButton("Debugen");
		btnDebugen.setIcon(new ImageIcon(Main.class.getResource("/das_java_forum_ide/pic/debug-step-into.png")));
		toolBar.add(btnDebugen);
		
		JButton btnSpeichern = new JButton("Speichern");
		btnSpeichern.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				
				JScrollPane s = (JScrollPane)tabbedPane.getSelectedComponent();
				JEditorPane e = (JEditorPane)  s.getViewport().getComponent(0);
				File f = new File(tabbedPane.getTitleAt(tabbedPane.getSelectedIndex()));
				
				
				try {
					
					FileWriter writer = new FileWriter(f);
					e.write(writer);
					
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
				
			}
		});
		btnSpeichern.setIcon(new ImageIcon(Main.class.getResource("/das_java_forum_ide/pic/document-save-as-2.png")));
		toolBar.add(btnSpeichern);
		
		JToolBar toolBar_1 = new JToolBar();
		contentPane.add(toolBar_1, BorderLayout.SOUTH);
		
		JButton btnndern = new JButton("\u00C4ndern");
		btnndern.setIcon(new ImageIcon(Main.class.getResource("/das_java_forum_ide/pic/document-save-as-4.png")));
		toolBar_1.add(btnndern);
		
		JSeparator separator_2 = new JSeparator();
		separator_2.setOrientation(SwingConstants.VERTICAL);
		toolBar_1.add(separator_2);
		
		JLabel lblArbeitsverzeichniss = new JLabel("Arbeitsverzeichniss:");
		toolBar_1.add(lblArbeitsverzeichniss);
		
		pathlbl = new JLabel();
		toolBar_1.add(pathlbl);
	}
	
	public static void loadProjekts(){
		File parentFile = new File(WorkingDir);
		DefaultMutableTreeNode node = new DefaultMutableTreeNode(parentFile.getPath());
		node.removeAllChildren();
		
		dirchanger.Main dc = new dirchanger.Main();
		node = dc.getdirs(node, parentFile);
						
		
		
		model.setRoot(node);
	}
	
	
	
	
	
	private void loadFile(File f){
		
		JScrollPane sc = new JScrollPane();
		JEditorPane editor = new JEditorPane();
		sc.setViewportView(editor);
		try {
			editor.setPage("File:///" + f.getPath());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		tabbedPane.add(f.getPath(),sc);
		
	}
	

}
